// my/wx.js
import {
  InvioceModel
} from '../../../models/invioce'
import {
  PayModel
} from '../../../models/pay'
import {
  Format
} from '../../../utils/common'
const payModel = new PayModel()
const invioceModel = new InvioceModel()
const app = getApp()
let last
const format = new Format()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    emailLists:[{mail: '',id:'',}]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getEmailList()
  },
  getEmailList() {
    invioceModel.getEmailList().then(res => {
      console.log(res)
      if (res && res.code == '00') {
        for (let i = 0; i < res.emailList.length; i++) {
          if(!res.emailList[i].mail){ 
    　　　　res.emailList[i].mail = ''
      　　}
        }
        this.setData({
          'emailLists': res.emailList
        })        
      } else {
        if (res &&  res.code ) {
          wx.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          wx.showToast({
            type: 'none',
            content: "获取邮箱列表失败",
            duration: 1000,
          });
        }
      }
    })
  },
  bindKeyInput(e) {
    console.log(e)
    this.data.emailLists[e.currentTarget.dataset.index].mail = e.detail.value
  },
  default(e) {
    let index = e.currentTarget.dataset.index
    let item = this.data.emailLists[index]
    if (item.state == 2 && !item.mail) {
      wx.showToast({
        type: 'none',
        content: '请选择已填写邮箱',
        duration: 1000,
      });
      return
    }
    if (item.state == 1) {
      this.data.emailLists[index].state = 2
    } else {
      for (let i = 0; i < this.data.emailLists.length; i++) {
        this.data.emailLists[i].state = 2
      } 
      this.data.emailLists[index].state = 1
    }
    
    this.setData({
      emailLists: this.data.emailLists
    })
  },
  toSave() {
    var regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
    for (let i = 0; i < this.data.emailLists.length; i++) {
      if (this.data.emailLists[i].state == 1 && !this.data.emailLists[i].mail) {
        wx.showToast({
          type: 'none',
          content: '默认邮箱不能为空',
          duration: 1000,
        });
        return
      }
      if(this.data.emailLists[i].mail && !regEmail.test(this.data.emailLists[i].mail)){ 
　　　   wx.showToast({
          type: 'none',
          content: '邮箱格式不正确',
          duration: 1000,
        });
　　　　return;
  　　}
    }
    let emailLists = this.data.emailLists
    invioceModel.saveInvoiceMail(emailLists[0].id,emailLists[0].mail,emailLists[0].state,emailLists[1].id,emailLists[1].mail,emailLists[1].state,emailLists[2].id,emailLists[2].mail,emailLists[2].state,emailLists[3].id,emailLists[3].mail,emailLists[3].state).then(res => {
      console.log(res)
      if (res && res.code == '00') {
        this.setData({
          'emailLists': res.emailList
        })
        wx.showToast({
          type: 'none',
          content: '设置成功',
          duration: 1000,
          success: () => {
            wx.navigateBack()
          },
        });
      } else {
        if (res &&  res.code ) {
          wx.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          wx.showToast({
            type: 'none',
            content: "获取邮箱列表失败",
            duration: 1000,
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})